package top.atcra.rpc.compress;

import top.atcra.rpc.compress.impl.DoNothingCompressor;
import top.atcra.rpc.compress.impl.GzipCompressor;
import top.atcra.rpc.config.ConfigLoader;

import java.util.HashMap;
import java.util.Map;

/**
 * 压缩器工厂类
 *
 * @author atcra
 * @date 2025-03-26
 */
public class CompressorFactory {

    private static final Map<CompressorType, Compressor> compressors = new HashMap<>() {{
        put(CompressorType.none, new DoNothingCompressor());
        put(CompressorType.gzip, new GzipCompressor());
    }};

    /**
     * 获取指定类型的压缩器
     *
     * @param type 压缩器类型
     * @return 压缩器
     */
    public static Compressor getCompressor(CompressorType type) {
        return compressors.get(type);
    }

    /**
     * 依据压缩器编号，获取指定类型的压缩器
     *
     * @param compressorId 指定的压缩器编号，详见<tt>CompressorType</tt>
     * @return 压缩器
     */
    public static Compressor getCompressor(byte compressorId) {
        var type = CompressorType.valueOf(compressorId);
        return getCompressor(type);
    }

    /**
     * 获取默认的压缩器
     *
     * @return Compressor
     */
    public static Compressor getCompressor() {
        var type = CompressorType.valueOf(ConfigLoader.getConfig().getCompressor());
        return getCompressor(type);
    }
}
